package net.mingsoft.document.biz.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.mingsoft.basic.core.SessionContext;
import net.mingsoft.basic.util.DateUtil;
import net.mingsoft.document.action.q.FileLogQuery;
import net.mingsoft.document.biz.FileLogBiz;
import net.mingsoft.document.dao.FileLogDao;
import net.mingsoft.document.entity.FileLogEntity;
import net.mingsoft.document.enums.FileOperateType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author gengfa
 * @date 22/01.28
 */
@Service
public class FileLogBizImpl implements FileLogBiz {
    @Resource
    FileLogDao fileLogDao;

    @Override
    public void record(String fileId, FileOperateType operateType) {
        String now = DateUtil.now();
        fileLogDao.insertOne(
                new FileLogEntity()
                        .setFileId(fileId)
                        .setOperateType(operateType.getType())
                        .setOperator(SessionContext.session().getIntegerId())
                        .setOperatorName(SessionContext.session().getManagerName())
                        .setCreatedBy(SessionContext.session().getId())
                        .setOperateDate(now)
                        .setCreatedDate(now)
        );
    }

    @Override
    public Page<FileLogEntity> list(FileLogQuery fileLogQuery) {
        Page<FileLogEntity> page = new Page<>(fileLogQuery.getPageNo(), fileLogQuery.getPageSize());
        fileLogDao.selectList(page, fileLogQuery.getFileId());
        page.getRecords().forEach(it -> {
            it.setOperateType(FileOperateType.getOpDesc(it.getOperateType()));
        });
        return page;
    }
}
